package p01_connection;

import Utils.db.DruidUtils;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class P01 {
    /**
     * 方式一
     * @throws SQLException
     */
    @Test
    public void test01() throws SQLException {
        Driver driver = new com.mysql.jdbc.Driver();
        String url = "jdbc:mysql://localhost:3307/test?useUnicode=true&characterEncoding=UTF-8";
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "ys19931111");
        Connection conn = driver.connect(url, info);
        System.out.println(conn);
    }

    /**
     * 方式二：使用反射
     */
    @Test
    public void test02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, SQLException {
        Class<?> driverClazz = Class.forName("com.mysql.jdbc.Driver");
//        Driver driver = (Driver) driverClazz.newInstance();
        Driver driver = (Driver) driverClazz.getDeclaredConstructor().newInstance();

        String url = "jdbc:mysql://localhost:3307/test?useUnicode=true&characterEncoding=UTF-8";
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "ys19931111");
        Connection conn = driver.connect(url, info);
        System.out.println(conn);
    }

    /**
     * 方法三：使用DriverManager替换Driver
     */
    @Test
    public void test03() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
        Class<?> driverClazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) driverClazz.getDeclaredConstructor().newInstance();
        String url = "jdbc:mysql://localhost:3307/test?useUnicode=true&characterEncoding=UTF-8";
        String user = "root";
        String password = "ys19931111";
        DriverManager.registerDriver(driver);

        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);
    }

    /**
     * 方法四
     */
    @Test
    public void test04() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
        String url = "jdbc:mysql://localhost:3307/test?useUnicode=true&characterEncoding=UTF-8";
        String user = "root";
        String password = "ys19931111";

        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);
    }

    /**
     * 方法五：不推荐
     */
    @Test
    public void test05() throws ClassNotFoundException, SQLException {
        String url = "jdbc:mysql://localhost:3307/test?useUnicode=true&characterEncoding=UTF-8";
        String user = "root";
        String password = "ys19931111";

//        Class.forName("com.mysql.jdbc.Driver"); // 可以注释掉此行，但是不建议
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);
    }

    /**
     * 配置文件
     */
    @Test
    public void test06() throws IOException, ClassNotFoundException, SQLException {
        InputStream is = P01.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties props = new Properties();
        props.load(is);

        String user = props.getProperty("user");
        String password = props.getProperty("password");
        String url = props.getProperty("url");
        String driverClassName = props.getProperty("driverClassName");

        Class.forName(driverClassName);
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);
    }

    @Test
    public void test07() throws Exception {
        Connection connection = DruidUtils.getInstance("jdbc.properties").getConnection();
        System.out.println(connection);
    }

    @Test
    public void test08() throws Exception {
        Connection connection = DruidUtils.getInstance("jdbc_book.properties").getConnection();
        System.out.println(connection);
    }
}
